//
//  MarketHomeController.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/18.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "MarketHomeController.h"
#import "MarketCycleImageCell.h"
#import "MarketHomeTouTiao.h"
#import "EveryDayTeHuiCell.h"
#import "OutOfTimeBuyCell.h"
#import "ImageViewADCell.h"

#import "ForYouRecomdItemCell.h"
#import "ForYouRecomdHeader.h"

#import "MarketSearchHeader.h"

#import "MarketHomeModel.h"
#import "MarketHSubModel.h"
#import "EmploymentDetailVC.h"
#import "RentHouseDetailVC.h"
#import "GoodDetailController.h"

#import "AboutusController.h"
@interface MarketHomeController ()<ImageViewADCellDelegate>
/** <#note#> */
@property (nonatomic, strong) NSArray *urls;

/** <#note#> */
@property (nonatomic, strong) MarketSearchHeader *headerView;

@property (nonatomic, strong) MarketHomeModel *dataModel;

/** <#note#> */
@property (nonatomic, assign) NSInteger section;

@end

@implementation MarketHomeController


- (MarketSearchHeader *)headerView{
    
    if (!_headerView) {
        
        MarketSearchHeader *temp = (MarketSearchHeader *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MarketSearchHeader class]) owner:nil options:nil].firstObject;
        
        temp.frame = CGRectMake(0, 0, KScreenW, 52);
        
        temp.type = @"3";
        
        _headerView = temp;
        
    }
    
    return _headerView;
}

- (NSArray *)urls{
    if (!_urls) {
        _urls = @[@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05530800562251838.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05530800562251838.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg"];
    }
    
    return _urls;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.groupTableView.tableHeaderView = self.headerView;
    
    self.headerView.hidden = YES;
    
    self.groupTableView.tableFooterView = [[UIView alloc]init];
    
    self.groupTableView.backgroundColor = [UIColor whiteColor];
    
    
    self.groupTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.groupTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.groupTableView.estimatedRowHeight = 100;
    
    

    [self.groupTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];

    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MarketCycleImageCell class]) bundle:nil] forCellReuseIdentifier:[MarketCycleImageCell identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MarketHomeTouTiao class]) bundle:nil] forCellReuseIdentifier:[MarketHomeTouTiao identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EveryDayTeHuiCell class]) bundle:nil] forCellReuseIdentifier:[EveryDayTeHuiCell identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([OutOfTimeBuyCell class]) bundle:nil] forCellReuseIdentifier:[OutOfTimeBuyCell identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ImageViewADCell class]) bundle:nil] forCellReuseIdentifier:[ImageViewADCell identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ForYouRecomdHeader class]) bundle:nil] forCellReuseIdentifier:[ForYouRecomdHeader identifierStr]];
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ForYouRecomdItemCell class]) bundle:nil] forCellReuseIdentifier:[ForYouRecomdItemCell identifierStr]];
    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.groupTableView.mj_header beginRefreshing];
    
    

    
    
    CGFloat navH = IPhoneX ? 88 : 64;
    CGFloat tabbarH = IPhoneX ? 83 : 49;
    
    if (@available(iOS 11.0, *)) {
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(XMGTitlesViewH, 0, 0, 0);
        
    }else{
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(navH + XMGTitlesViewH, 0, tabbarH,0 );
        
        
    }
    // Do any additional setup after loading the view.
}


- (void)loadData{
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Mall/index" parameters:nil completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            MYJSONSTR
            
            weakSelf.dataModel = [MarketHomeModel mj_objectWithKeyValues:responseObject[@"data"]];
            
//            weakSelf.dataModel.tehui = nil;
            
            weakSelf.dataModel.articles = nil;
            
            
            if (weakSelf.dataModel.advs || weakSelf.dataModel.tuijianadvs) {
                
                [weakSelf downloadImage];
                
            }else{
                
                
                if ( weakSelf.dataModel.advs == nil &&  weakSelf.dataModel.articles == nil &&  weakSelf.dataModel.tehui == nil &&  weakSelf.dataModel.xianshi == nil &&  weakSelf.dataModel.tuijianadvs == nil  &&  weakSelf.dataModel.tuijian == nil) {
                    
                    weakSelf.logoView.hidden = NO;
                    
                } else {
                    
                    weakSelf.headerView.hidden = NO;
                }
                
                
                
                
                [weakSelf.groupTableView.mj_header endRefreshing];
                
                [weakSelf.groupTableView reloadData];
            }
                
            
            
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
            [weakSelf.groupTableView.mj_header endRefreshing];
        }
        
        
    }];
    
}



- (void)downloadImage{
    
    dispatch_group_t group = dispatch_group_create();
    
    
    if (self.dataModel.advs) {
        
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.dataModel.advs.firstObject.adv_image] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            
            dispatch_group_leave(group);
            
        }];
    }
    
    
    
    
    if (self.dataModel.tuijianadvs) {
        
        dispatch_group_enter(group);
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.dataModel.tuijianadvs.firstObject.adv_image] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            dispatch_group_leave(group);
            
        }];
    }
    
    
    
    
    MJWeakSelf;
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
  
        if ( weakSelf.dataModel.advs == nil &&  weakSelf.dataModel.articles == nil &&  weakSelf.dataModel.tehui == nil &&  weakSelf.dataModel.xianshi == nil &&  weakSelf.dataModel.tuijianadvs == nil  &&  weakSelf.dataModel.tuijian == nil) {
            
            weakSelf.logoView.hidden = NO;
            
        } else {
            
             weakSelf.headerView.hidden = NO;
        }
        
       
        
        


        
        [weakSelf.groupTableView.mj_header endRefreshing];
        
        [weakSelf.groupTableView reloadData];
        
        
        
    });
    
    
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModel == nil) {
        
        return 0;
        
    } else {
        
        NSInteger count = 0;
        
        if (self.dataModel.advs) {
            count++;
        }
        
        if (self.dataModel.articles) {
            count++;
        }
        
        
        if (self.dataModel.tehui) {
            count++;
        }
        
        if (self.dataModel.xianshi) {
            count++;
        }
        
        if (self.dataModel.tuijianadvs) {
            count++;
        }
        
        if (self.dataModel.tuijian) {
            count++;
        }
        
        self.section = count;
        
        return count;
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.dataModel.tuijian) {
        
        if (section == self.section - 1) {
            return 2;
        } else {
            return 1;
        }
        
    }else{
        
        
        return 1;
    }
    
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (self.dataModel.advs) {
            
            MarketCycleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:[MarketCycleImageCell identifierStr]];
            
            cell.model = self.dataModel;
            
            return cell;
            
        }
        
        if (self.dataModel.articles) {
            
            MarketHomeTouTiao *cell = [tableView dequeueReusableCellWithIdentifier:[MarketHomeTouTiao identifierStr]];
            
            cell.model = self.dataModel;

            return cell;
            
        }
        
        
        if (self.dataModel.tehui) {
            EveryDayTeHuiCell *cell = [tableView dequeueReusableCellWithIdentifier:[EveryDayTeHuiCell identifierStr]];
            cell.model = self.dataModel;

            
            return cell;
            
        }
        
        if (self.dataModel.xianshi) {
           
            OutOfTimeBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:[OutOfTimeBuyCell identifierStr]];
            cell.model = self.dataModel;

            
            return cell;
        }
        
        if (self.dataModel.tuijianadvs) {
            
            ImageViewADCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageViewADCell identifierStr]];
            cell.model = self.dataModel;

            cell.delegate = self;

            return cell;
            
        }
        
        if (self.dataModel.tuijian) {
            if (indexPath.row == 0) {
                
                
                ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];

                
                return cell;
            } else {
                
                ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
            }
        }
        
  
        
    }else if (indexPath.section == 1) {
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
            if (index == 1) {
                
              MarketHomeTouTiao *cell = [tableView dequeueReusableCellWithIdentifier:[MarketHomeTouTiao identifierStr]];
                cell.model = self.dataModel;

                
                return cell;
                
                
            }
            
            index++;
            
            
        }
        
        if (self.dataModel.tehui) {
            
            if (index == 1) {
                
                EveryDayTeHuiCell *cell = [tableView dequeueReusableCellWithIdentifier:[EveryDayTeHuiCell identifierStr]];
                cell.model = self.dataModel;

                
                return cell;
                
                
            }
            
            index++;
        }
        
        if (self.dataModel.xianshi) {
            
            if (index == 1) {
                
                OutOfTimeBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:[OutOfTimeBuyCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
                
            }
            
            index++;
        }
        
        if (self.dataModel.tuijianadvs) {
             if (index == 1) {
            ImageViewADCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageViewADCell identifierStr]];
                 cell.model = self.dataModel;
                 cell.delegate = self;
            
            return cell;
             }
            
            index++;
        }
        
        
        if (self.dataModel.tuijian) {
            
            if (indexPath.row == 0) {
                
                
                ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];
                

                return cell;
            } else {
                
                ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];

                cell.model = self.dataModel;

                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
//            MarketHomeTouTiao *cell = [tableView dequeueReusableCellWithIdentifier:[MarketHomeTouTiao identifierStr]];
//
//
//            return cell;
//
      
        
        
    }else if (indexPath.section == 2) {
        
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
          
            index++;
            
            
        }
        
        if (self.dataModel.tehui) {
            
            if (index == 2) {
                
                EveryDayTeHuiCell *cell = [tableView dequeueReusableCellWithIdentifier:[EveryDayTeHuiCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
                
                
            }
            
            index++;
        }
        
        if (self.dataModel.xianshi) {
            
            if (index == 2) {
                
                OutOfTimeBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:[OutOfTimeBuyCell identifierStr]];
                cell.model = self.dataModel;

                
                return cell;
                
            }
            
            index++;
        }
        
        if (self.dataModel.tuijianadvs) {
            if (index == 2) {
                ImageViewADCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageViewADCell identifierStr]];
                
                cell.model = self.dataModel;
                cell.delegate = self;

                return cell;
            }
            
            index++;
        }
        
        
        if (self.dataModel.tuijian) {
            
            if (indexPath.row == 0) {
                
                
                ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];
                
                
                return cell;
            } else {
                
                ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
        
        
    }else if (indexPath.section == 3) {
        

        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
            
            index++;
            
            
        }
        
        if (self.dataModel.tehui) {
            
         
            
            index++;
        }
        
        if (self.dataModel.xianshi) {
            
            if (index == 3) {
                
                OutOfTimeBuyCell *cell = [tableView dequeueReusableCellWithIdentifier:[OutOfTimeBuyCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
                
            }
            
            index++;
        }
        
        if (self.dataModel.tuijianadvs) {
            if (index == 3) {
                ImageViewADCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageViewADCell identifierStr]];
                
                cell.model = self.dataModel;
                cell.delegate = self;

                return cell;
            }
            
            index++;
        }
        
        
        if (self.dataModel.tuijian) {
            
            if (indexPath.row == 0) {
                
                
                ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];
                
                
                return cell;
            } else {
                
                ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];
                
                cell.model = self.dataModel;

                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
      
        
        
        
    }else if (indexPath.section == 4) {
        
        
     
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
            
            index++;
            
            
        }
        
        if (self.dataModel.tehui) {
            
            
            
            index++;
        }
        
        if (self.dataModel.xianshi) {
            
         
            index++;
        }
        
        if (self.dataModel.tuijianadvs) {
            if (index == 4) {
                ImageViewADCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageViewADCell identifierStr]];
                
                cell.model = self.dataModel;
                cell.delegate = self;

                return cell;
            }
            
            index++;
        }
        
        
        if (self.dataModel.tuijian) {
            
            if (indexPath.row == 0) {
                
                
                ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];
                
                
                return cell;
            } else {
                
                ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];
                cell.model = self.dataModel;

                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
        
        
        
        
        
        
        
    }else{
        
        if (indexPath.row == 0) {
            
            
            ForYouRecomdHeader *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdHeader identifierStr]];
            
            
            return cell;
        } else {
            
            ForYouRecomdItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[ForYouRecomdItemCell identifierStr]];
            cell.model = self.dataModel;

            
            return cell;
        }
        
        
        

        
        
    }
    
    
    
    
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
    
    return cell;
    
    
}


- (void)imageTopBtnDidClickWith:(NSArray<MarketHSubModel *> *)models{
    
         MarketHSubModel *subModel = models.firstObject;
    
        if ([subModel.class_id isEqualToString:@"1"]) {
    
            RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
    
            temp.houseId = subModel.ID;
    
            [self.navigationController pushViewController:temp animated:YES];
            

        } else if ([subModel.class_id isEqualToString:@"2"]){
    
            EmploymentDetailVC *tempVC = [[EmploymentDetailVC alloc]init];
    
            tempVC.IdStr = subModel.ID;
    
            [self.navigationController pushViewController:tempVC animated:YES];
    
        }else if ([subModel.class_id isEqualToString:@"3"]){
    
            GoodDetailController *tempVC = [[GoodDetailController alloc]init];
    
            OneGoodsModel *tempModel = [[OneGoodsModel alloc]init];
    
            tempModel.goods_id = subModel.ID;
    
            tempModel.goods_image_url = subModel.adv_image;
    
            tempVC.goodsModel = tempModel;
    
            [self.navigationController pushViewController:tempVC animated:YES];
    
    
        }else{
            
            AboutusController *temp = [[AboutusController alloc]init];
            
            temp.urlString = subModel.content;
            
            [self.navigationController pushViewController:temp animated:YES];
    
    
        }
    
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.00001;
    }else{
        
        if (self.dataModel.articles && self.dataModel.tehui) {
            
            if (self.dataModel.advs) {
                
                if (section == 2) {
                    
                    return 0.00001;
                    
                } else {
                    
                    if (section == 1) {
                        return 8;
                    } else {
                        return 16;
                    }
                    
                    
                }
                
            } else {
                
                if (section == 1) {
                    return 0.00001;
                } else {
                    return 16;
                }
            }
            
        } else {
            
            if (self.dataModel.articles) {
                if (self.dataModel.advs) {
                    if (section == 1) {
                        return 8;
                    } else {
                        return 16;
                    }
                } else {
                    return 16;
                }
            } else {
                return 16;
            }
            
        }
        
        
    }
    
    

    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.00001;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    
    UIView *temp = [[UIView alloc]init];
    
    temp.backgroundColor = colorWith(241, 242, 244);
    
    return temp;
    
    
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return [[UIView alloc]init];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
